package com.nsu.konus.control.preferences;

import java.io.File;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Preferences;
import org.osgi.framework.Bundle;

import com.nsu.konus.control.Activator;
import com.nsu.konus.control.IKonusParameters;

public class KonusPreference implements IKonusParameters{
	
	private Preferences prefs;
	
	private static final String PACKAGE_NAME = "com.nsu.konus.control."; 
	
	public static final String KONUS_PATH = PACKAGE_NAME + "konusPath";
	
	public static final String RUSSIFIER_PATH = PACKAGE_NAME + "russifierPath";
	
	public static final String DOSBOX_PATH = PACKAGE_NAME + "dosboxPath";
	
	public static final String SOLVER_WORKING_TIME = PACKAGE_NAME + "workingTime";
	
	public static final String PARAMETRERS_DATA_NAME = PACKAGE_NAME + "parametersDataName";
	
	public static final String FORTRAN_SOURCE_NAME = PACKAGE_NAME + "fortranSourceName";
	
	public static final String SOLVER_NAME = PACKAGE_NAME + "solverName";
	
	public static final String RESULT_FILE_NAME = PACKAGE_NAME + "resultFileName";
	
	public static final String FORTRAN_PATH = PACKAGE_NAME + "fortranPath";
	
	public static final String USE_DEFAULT = PACKAGE_NAME + "useDefault";
	
	private static KonusPreference DefaultPreferences;
	
	public KonusPreference(){
		prefs = new Preferences();
	}

	public KonusPreference(Preferences prefs){
		this.prefs = prefs;
	}
	
	public static KonusPreference getDefault() {
		if (DefaultPreferences == null) {
			DefaultPreferences = new KonusPreference();
			DefaultPreferences.setDefaultValues();
		}
		return DefaultPreferences;
	}
	
	public static KonusPreference getInstance() {
		return new KonusPreference(Activator.getDefault().getPluginPreferences());
	}

	/* (non-Javadoc)
	 * @see com.nsu.konus.control.IKonusParameters#getKonusPath()
	 */
	public String getKonusPath() {
		return prefs.getString(KONUS_PATH);
	}

	/* (non-Javadoc)
	 * @see com.nsu.konus.control.IKonusParameters#getRussifierPath()
	 */
	public String getRussifierPath() {
		return prefs.getString(RUSSIFIER_PATH);
	}

	/* (non-Javadoc)
	 * @see com.nsu.konus.control.IKonusParameters#getDosboxPath()
	 */
	public String getDosboxPath() {
		return prefs.getString(DOSBOX_PATH);
	}

	/* (non-Javadoc)
	 * @see com.nsu.konus.control.IKonusParameters#solverWorkingTime()
	 */
	public int getSolverWorkingTime() {
		return prefs.getInt(SOLVER_WORKING_TIME);
	}

	/* (non-Javadoc)
	 * @see com.nsu.konus.control.IKonusParameters#getParametersDataName()
	 */
	public String getParametersDataName() {
		return prefs.getString(PARAMETRERS_DATA_NAME);
	}

	/* (non-Javadoc)
	 * @see com.nsu.konus.control.IKonusParameters#getFortranSourceName()
	 */
	public String getFortranSourceName() {
		return prefs.getString(FORTRAN_SOURCE_NAME);
	}

	/* (non-Javadoc)
	 * @see com.nsu.konus.control.IKonusParameters#getSolverName()
	 */
	public String getSolverName() {
		return prefs.getString(SOLVER_NAME);
	}

	/* (non-Javadoc)
	 * @see com.nsu.konus.control.IKonusParameters#getFortranPath()
	 */
	public String getFortranPath() {
		return prefs.getString(FORTRAN_PATH);
	}
	
	public boolean isUseDefault() {
		return prefs.getBoolean(USE_DEFAULT);
	}
	
	/* (non-Javadoc)
	 * @see com.nsu.konus.control.IKonusParameters#getResultFileName()
	 */
	public String getResultFileName() {
		return prefs.getString(RESULT_FILE_NAME);
	}
	
	public void setKonusPath(String konusPath) {
		prefs.setValue(KONUS_PATH, konusPath);
	}

	public void setRussifierPath(String russifierPath) {
		prefs.setValue(RUSSIFIER_PATH, russifierPath);
	}

	public void setDosboxPath(String dosboxPath) {
		prefs.setValue(DOSBOX_PATH, dosboxPath);
	}

	public void setSolverWorkingTime(int solverWorkingTime) {
		prefs.setValue(SOLVER_WORKING_TIME, solverWorkingTime);
	}

	public void setParametersDataName(String parametersDataName) {
		prefs.setValue(PARAMETRERS_DATA_NAME, parametersDataName);
	}

	public void setFortranSourceName(String fortranSourceName) {
		prefs.setValue(FORTRAN_SOURCE_NAME, fortranSourceName);
	}

	public void setSolverName(String solverName) {
		prefs.setValue(SOLVER_NAME, solverName);
	}

	public void setResultFileName(String resultFileName) {
		prefs.setValue(RESULT_FILE_NAME, resultFileName);
	}

	public void setFortranPath(String fortranPath) {
		prefs.setValue(FORTRAN_PATH, fortranPath);
	}
	
	public void setUseDefault(boolean useDefault) {
		prefs.setValue(USE_DEFAULT, useDefault);
	}
	
	public void setValue(String name, int value) {
		prefs.setValue(PACKAGE_NAME + name, value);
	}
	
	public void setValue(String name, String value) {
		prefs.setValue(PACKAGE_NAME + name, value);
	}
	
	public void setDefaultValues() {
		try {
			Bundle bundle = Activator.getDefault().getBundle();
			String pluginPath = FileLocator.resolve(bundle.getEntry("")).getFile();
			String corePath = getPath(pluginPath, "core");
			String utilPath = getPath(pluginPath, "util");
			
			prefs.setDefault(KONUS_PATH, corePath);
			prefs.setDefault(RUSSIFIER_PATH, getPath(utilPath, "dosboxrus"));
			prefs.setDefault(DOSBOX_PATH, getPath(utilPath, "dosbox\\dosbox.exe"));
			prefs.setDefault(SOLVER_WORKING_TIME, 1);
			prefs.setValue(FORTRAN_PATH, getPath(corePath, "FORT"));
			prefs.setValue(USE_DEFAULT, true);
		}
		catch(Exception e) {
		}
	}
	
	private String getPath(String first, String last) {
		return new File(first, last).getAbsolutePath();
	}

}
